package cn.zn.framework.printer.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.zn.framework.common.exception.RRException;
import cn.zn.framework.common.vo.FinanceValues;
import cn.zn.framework.printer.dto.PrinterItemDTO;
import cn.zn.framework.printer.enums.XYReturnCodeEnum;
import cn.zn.framework.printer.inter.PrinterInter;
import lombok.extern.slf4j.Slf4j;
import net.xpyun.platform.opensdk.service.PrintService;
import net.xpyun.platform.opensdk.util.HashSignUtil;
import net.xpyun.platform.opensdk.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PrinterServiceImpl implements PrinterInter {

    @Autowired
    private FinanceValues financeValues;
    private PrintService printService = new PrintService();

    @Override
    public void addPrinters(PrinterItemDTO dto) {
        AddPrinterRequest printerRequest = new AddPrinterRequest();
        printerRequest.setUser(this.getUser());
        printerRequest.setTimestamp(String.valueOf(System.currentTimeMillis()));
        printerRequest.setSign(this.getSign(printerRequest.getTimestamp()));
        printerRequest.setDebug("0");
        printerRequest.setItems(new AddPrinterRequestItem[]{AddPrinterRequestItem.builder()
                .name(dto.getPrinterName())
                .sn(dto.getPrinterSn())
                .build()});
        ObjectRestResponse<PrinterResult> restResponse = printService.addPrinters(printerRequest);
        if (restResponse.getCode() == 0) {
            PrinterResult data = restResponse.getData();
            if (!data.getFailMsg().isEmpty()) {
                log.error("调用打印接口错误，data:{}", data);
                String[] msgArr = data.getFailMsg().get(0).split(":");
                XYReturnCodeEnum xyReturnCodeEnum = XYReturnCodeEnum.findByCode(msgArr[1]);
                throw new RRException(msgArr[0].concat(",").concat(xyReturnCodeEnum == null ? msgArr[1] : xyReturnCodeEnum.getMsg()));
            }
        } else {
            log.error("调用打印接口错误，参数:{},code:{},msg:{}", printerRequest, restResponse.getCode(), restResponse.getMsg());
            throw new RRException("调用打印接口失败");
        }
    }

    @Override
    public int queryPrinterStatus(PrinterItemDTO dto) {
        Integer status = 0;
        PrinterRequest printerRequest = new PrinterRequest();
        if (StrUtil.isNotBlank(dto.getPrinterSn())) {
            printerRequest.setUser("zhunahao@qq.com");
            printerRequest.setTimestamp(String.valueOf(System.currentTimeMillis()));
            printerRequest.setSign(HashSignUtil.sign(printerRequest.getUser() + "bea0a379265c48e4be5e701e0cd82f32" + printerRequest.getTimestamp()));
            printerRequest.setSn(dto.getPrinterSn());
            printerRequest.setDebug("0");
            ObjectRestResponse<Integer> restResponse = printService.queryPrinterStatus(printerRequest);
            if (restResponse.getCode() == 0) {
                status = restResponse.getData();
            } else {
                log.error("调用打印接口错误，参数:{},code:{},msg:{}", printerRequest, restResponse.getCode(), restResponse.getMsg());
                throw new RRException("调用打印接口失败");
            }
        }
        return status;
    }

    @Override
    public void print(String sn, String content) {
        PrintRequest printerRequest = new PrintRequest();
        printerRequest.setUser(this.getUser());
        printerRequest.setTimestamp(String.valueOf(System.currentTimeMillis()));
        printerRequest.setSign(this.getSign(printerRequest.getTimestamp()));
        printerRequest.setDebug("0");
        printerRequest.setSn(sn);
        printerRequest.setContent(content);
        ObjectRestResponse<String> restResponse = printService.print(printerRequest);
        if (restResponse.getCode() == 0) {
            String data = restResponse.getData();
            log.debug("data:{}", data);
        } else {
            log.error("调用打印接口错误，参数:{},code:{},msg:{}", printerRequest, restResponse.getCode(), restResponse.getMsg());
            throw new RRException("调用打印接口失败");
        }
    }

    private String getUser() {
        return financeValues.getPrinter().getXyUser();
    }

    private String getSign(String timestamp) {
        return HashSignUtil.sign(financeValues.getPrinter().getXyUser().concat(financeValues.getPrinter().getXySecret()).concat(timestamp));
    }
}
